/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.opencfg.lang;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * <p>
 * DateFormatUtils
 * </p>
 * 
 * @author reymondtu@gmail.com
 * @sicene 0.0.1-SNAPSHOT
 * @version $Id: DateFormatUtils.java 2011-9-28 $
 */
public final class DateFormatUtils {
	
	private static final String _properties_file_name = "DateFormatUtils.properties";

	private static final HashMap<String, SimpleDateFormat> _partenMap = new HashMap<String, SimpleDateFormat>();
	
	static {
		// load data parten properties files
		try {
			List<String> propList = ResourcesUtils
					.getClassPathResource(_properties_file_name);
			for (String dateParten : propList) {
				if (StringUtils.startsWith(dateParten, "#"))
					continue;
				_partenMap.put(StringUtils.trim(dateParten),
						new SimpleDateFormat(dateParten));
			}
		} catch (IOException | URISyntaxException e) {
			e.printStackTrace();
		}
	}

	/**
	 * String -> Date
	 * 
	 * @param dateParten
	 *            Parse Date Parten
	 * @param dateStr
	 *            Parse Date String
	 * @return Parsed java.util.Date           
	 * @throws ParseException
	 */
	public static Date safeParseDate(final String dateParten,
			final String dateStr) throws ParseException {
		return getFormat(dateParten).parse(dateStr);
	}

	/**
	 * Date -> String
	 * 
	 * @param dateParten
	 *            Format Date Parten
	 * @param date
	 *            Format java.util.Date Object
	 * @return Formated Date String
	 */
	public static String safeFormatDate(final String dateParten, final Date date) {
		return getFormat(dateParten).format(date);
	}
	
	private static DateFormat getFormat(final String dateParten) {
		return (DateFormat) _threadLocal.get().get(dateParten);
	}

	private static ThreadLocal<HashMap<String, SimpleDateFormat>> _threadLocal = new ThreadLocal<HashMap<String, SimpleDateFormat>>() {
		@SuppressWarnings("unchecked")
		protected synchronized HashMap<String, SimpleDateFormat> initialValue() {
			return (HashMap<String, SimpleDateFormat>) _partenMap.clone();
		}
	};

}
